package com.buss.admin.controller;

import java.util.ArrayList;
import java.util.List;

import com.buss.admin.entity.RoleFunctionEntity;
import com.buss.admin.entity.UserRoleEntity;
import com.buss.auth.transfer.vo.authen.AuthUserVOAdmin;
import com.buss.common.service.SysServiceI;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.jweb.core.bean.PageMode;
import org.jweb.core.query.hibernate.qbc.CriteriaQuery;
import org.jweb.core.util.MyBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.buss.admin.entity.AuthFunctionEntity;
import com.buss.admin.service.IAuthFunctionService;
import com.buss.admin.trans.form.FunctionForm;
import com.buss.common.model.json.EasyUIDataGridResult;
import com.buss.common.model.json.HXResult;
import com.buss.common.util.IDUtils;

@RequestMapping("admin/function")
@Controller
public class FunctionController {

	@Autowired
	private SysServiceI sysService;
	@Autowired
	private IAuthFunctionService functionService;

	@RequestMapping("/getmenulist")
	@ResponseBody
	public List<AuthFunctionEntity> getMenuList(){
		List<AuthFunctionEntity> list = new ArrayList<>();

		// 获得后台用户信息
		Subject currentUser = SecurityUtils.getSubject();
		AuthUserVOAdmin user = (AuthUserVOAdmin) currentUser.getSession().getAttribute("userInfoAdmin");
		String uid = user.getId();

		List<UserRoleEntity> roleList = sysService.findByProperty(UserRoleEntity.class, "user.id", uid);
		if (roleList!=null && roleList.size()>0) {
			String roleid = roleList.get(0).getRole().getId();
			String rolecode = roleList.get(0).getRole().getCode();
			// 如果角色名是admin，则查所有菜单
			if("admin".equals(rolecode)){
				list = functionService.getList(AuthFunctionEntity.class);
			}else {
				// 查出管理员预先配置的角色-菜单
				List<RoleFunctionEntity> lista = sysService.findByProperty(RoleFunctionEntity.class, "role.id", roleid);
				AuthFunctionEntity role = null;

				for (RoleFunctionEntity aa : lista){
					role = new AuthFunctionEntity();
					MyBeanUtils.copyBeanNotNull2Bean(aa.getFunctionEntity(),role);
					list.add(role);
				}
			}
		}else{
			// 如果没有角色，则获取所有菜单
			list = functionService.getList(AuthFunctionEntity.class);
		}

		// 获取所有菜单
//		List<AuthFunctionEntity> list1 = functionService.getList(AuthFunctionEntity.class);
		return list;
	}

	@RequestMapping("/getmenulistall")
	@ResponseBody
	public List<AuthFunctionEntity> getMenuListAll() {
		List<AuthFunctionEntity> list = functionService.getList(AuthFunctionEntity.class);
		return list;
	}

	@RequestMapping("/getMymnulistall")
	@ResponseBody
	public List<AuthFunctionEntity> getMymnulistall(String roleid) {
		List<RoleFunctionEntity> lista = sysService.findByProperty(RoleFunctionEntity.class, "role.id", roleid);
		AuthFunctionEntity role = null;
		List<AuthFunctionEntity> list = new ArrayList<>();
		for (RoleFunctionEntity aa : lista){
			role = new AuthFunctionEntity();
			MyBeanUtils.copyBeanNotNull2Bean(aa.getFunctionEntity(),role);
			list.add(role);
		}
//		List<AuthFunctionEntity> list = functionService.getList(AuthFunctionEntity.class);
		return list;
	}

	@RequestMapping("/list")
	@ResponseBody
	public EasyUIDataGridResult getFunctionList(FunctionForm functionForm) {

		// 分页查询
		CriteriaQuery cq = functionForm.getCriteriaQuery();

		/*
		 * 设置查询条件
		 */
		// cq.add(Restrictions.sizeGe("checkstatus", 10));
		// cq.add(Restrictions.ge("checkstatus", 10));

		int pageNo = Integer.parseInt(functionForm.getPage());
		int pageSize = Integer.parseInt(functionForm.getRows());

		// 定义返回数据格式
		EasyUIDataGridResult result = new EasyUIDataGridResult();
		PageMode<AuthFunctionEntity> pageMode = functionService.list4page(cq, pageNo, pageSize);

		// 封装数据
		result.setRows(pageMode.getResult());
		result.setTotal(pageMode.getTotal());

		return result;

	}

	@RequestMapping("/saveorupdate")
	@ResponseBody
	public HXResult functionAdd(AuthFunctionEntity functionEntity) {

		if ("".equals(functionEntity.getpId())) {
			functionEntity.setpId(null);
		}

		try {

			if (functionEntity.getId() == null) {

				// 设置id
				String id = IDUtils.getNumStringId();
				functionEntity.setId(id);

			}

			// 插入数据
			functionService.saveOrUpdate(functionEntity);

			return HXResult.ok("操作权限成功!");
		} catch (Exception e) {
			e.printStackTrace();
			return HXResult.build(500, "服务器繁忙，操作权限失败！");

		}

	}

	/*
	 * 管理员相关页面跳转
	 */
	@RequestMapping("/{path}")
	public String goPath(@PathVariable String path) {
		return "admin/function/" + path;
	}
}
